//
//  ATRAZMap_CollectionCell.m
//  testImage
//
//  Created by lianglibao on 2018/8/31.
//  Copyright © 2018年 lianglibao. All rights reserved.
//

#import "ATRAZMap_CollectionCell.h"
#import "ATRAZMapCardModel.h"
#import "ATScreenAdapter.h"
#import "UIButton+ATUtil.h"
#import "UIView+ATFrame.h"
#import "ATLargeImageView.h"
#import "ATUtil.h"

@interface ATRAZMap_CollectionCell ()
@property (nonatomic, strong) NSMutableArray *cardButtons;
@property (nonatomic, strong) NSMutableArray *cardLabels;
@property (nonatomic, strong) CALayer *bgCoverLayer;          // 主题之间衔接处背景layer.
@property (nonatomic, strong) ATLargeImageView *bgImageView;
@end


@implementation ATRAZMap_CollectionCell

- (ATLargeImageView *)bgImageView:(id)name {
    if (!_bgImageView) {
        _bgImageView = [[ATLargeImageView alloc] initWithImageInfo:name
                                                         andTileCount:16];
    } else {
        _bgImageView.imageInfo = name;
    }
    return _bgImageView;
}

- (CALayer *)bgCoverLayer {
    if (!_bgCoverLayer) {
        _bgCoverLayer = [CALayer layer];
        _bgCoverLayer.opaque = YES;
        _bgCoverLayer.opacity = 1;
        _bgCoverLayer.shouldRasterize = YES;
        _bgCoverLayer.allowsEdgeAntialiasing = YES;
        _bgCoverLayer.masksToBounds = NO;
    }
    return _bgCoverLayer;
}


- (NSMutableArray *)cardButtons {
    if (!_cardButtons) {
        _cardButtons = [NSMutableArray arrayWithCapacity:10];
    }
    return _cardButtons;
}

- (NSMutableArray *)cardLabels {
    if (!_cardLabels) {
        _cardLabels = [NSMutableArray array];
    }
    return _cardLabels;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupSubViews];
    }
    
    return self;
}

- (void)setupSubViews {
    [self.contentView.layer addSublayer:self.bgCoverLayer];
    
    for (int i = 0; i < 33; i++) {
        UIButton *button = [UIButton new];
        button.hidden = YES;
        button.exclusiveTouch = YES;
        [button addTarget:self action:@selector(onTapCardAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.cardButtons addObject:button];
        [self.contentView addSubview:button];
        
        UILabel *cardLabel = [UILabel new];
        cardLabel.hidden = YES;
        if ([ATUtil isiPad]) {
            cardLabel.font = [UIFont systemFontOfSize:16];
        } else {
            cardLabel.font = [UIFont systemFontOfSize:14];
        }
        
        cardLabel.textColor = [UIColor whiteColor];
        cardLabel.textAlignment = NSTextAlignmentCenter;
        [self.cardLabels addObject:cardLabel];
        [self.contentView addSubview:cardLabel];
    }
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.bgCoverLayer.frame = CGRectMake(-1,
                                         0,
                                         2.5,
                                         self.contentView.bounds.size.height
                                         );
}

- (void)setCardModel:(ATRAZMapCardModel *)cardModel {
    _cardModel = cardModel;
    
    if (!self.bgImageView) {
        [self.contentView addSubview:[self bgImageView:cardModel.cardBgImageName]];
        self.bgImageView.width = cardModel.subjectWidth;
        self.bgImageView.height = ATSCREEN_HEIGHT();
    } else {
        [self bgImageView:cardModel.cardBgImageName];
        self.bgImageView.width = cardModel.subjectWidth;
        self.bgImageView.height = ATSCREEN_HEIGHT();
    }
    
    // 1.先隐藏掉所有的子控件(只有在不是同等数量的主题才需要)
    if (cardModel.cardStartIndex == 155) {
        [self.cardButtons enumerateObjectsUsingBlock:^(UIButton *obj,
                                                       NSUInteger idx,
                                                       BOOL * _Nonnull stop) {
            obj.hidden = YES;
            
        }];
        
        [self.cardLabels enumerateObjectsUsingBlock:^(UILabel *obj,
                                                      NSUInteger idx,
                                                      BOOL * _Nonnull stop) {
            obj.hidden = YES;
        }];
    }
    CGFloat scale = [UIScreen mainScreen].scale;
    // 2.在对所有的子控件重新赋值
    @autoreleasepool {
        UIImage *bgLayerImage = cardModel.bgLayerImage;
        self.bgCoverLayer.contents = (id)bgLayerImage.CGImage;
        self.bgCoverLayer.hidden = NO;
        
        __block NSInteger tag = cardModel.cardStartIndex;
        __block UIImage *buttonDontImage    = cardModel.cardDontImage;
        __block UIImage *buttonSuccessImage = cardModel.cardDoneImage;
        __block NSInteger curPass = 160;
        __block UIImage *doneImage;
        __block UIImage *dontImage;
        
        __weak typeof(self) weakSelf = self;
        
        [cardModel.cardCenterFrame enumerateObjectsUsingBlock:^(NSString *centerFrameStr,
                                                                NSUInteger idx,
                                                                BOOL *_Nonnull stop) {
            UIButton *card = weakSelf.cardButtons[idx];
            card.tag = tag;
            CGRect centerFrame = CGRectFromString(centerFrameStr);
            if ([ATUtil isiPad]) {
                card.size = CGSizeMake(centerFrame.size.width * 2, centerFrame.size.height * 2);
            } else {
                card.size = centerFrame.size;
            }
            card.center = centerFrame.origin;
            card.hidden = NO;
            
            UILabel *numLabel = weakSelf.cardLabels[idx];
            numLabel.text = @(tag + 1).stringValue;
            numLabel.size = CGSizeMake(centerFrame.size.width, 0);
            numLabel.numberOfLines = 1;
            numLabel.adjustsFontSizeToFitWidth = YES;
            [numLabel sizeToFit];
            numLabel.centerX = centerFrame.origin.x;
            numLabel.top = centerFrame.origin.y + 5;
            numLabel.hidden = NO;
            
            // 当前关卡没有完成或进行中
            if (tag > curPass) {
                card.shouldClick = @(NO);
                [card setImage:buttonDontImage forState:UIControlStateNormal];
                numLabel.top = card.bottom - ATFloat(16);
                // 进行中
            } else if (tag == curPass) {
                card.shouldClick = @(YES);
                [card setImage:buttonSuccessImage forState:UIControlStateNormal];
                // 已完成
            } else {
                card.shouldClick = @(YES);
                [card setImage:buttonSuccessImage forState:UIControlStateNormal];
            }
            
            // 第1个主题
            if (cardModel.cardStartIndex == 0) {
                // 特殊关卡,第0关,第10关,第20关,第30关
                if ((tag == 0) || (tag == 9) || (tag == 19) || (tag == 29)) {
                    // 第0关没有不可点击..
                    doneImage = cardModel.cardSuperDoneImage;
                    dontImage = cardModel.cardSuperDontImage;
                    if ([card.shouldClick boolValue]) {
                        [card setImage:doneImage forState:UIControlStateNormal];
                        if ([ATUtil isiPad]) {
                            numLabel.top = centerFrame.origin.y + 12;
                        }
                    } else {
                        [card setImage:dontImage forState:UIControlStateNormal];
                        numLabel.top = card.bottom - ATFloat(14);
                    }
                    
                    // 特殊关卡,第4关.(站台关卡)
                } else if (tag == 3) {
                    card.hidden = NO;
                    [card setImage:cardModel.cardStationImage forState:UIControlStateNormal];
                    if ([ATUtil isiPad]) {
                        numLabel.top = centerFrame.origin.y + 20;
                    } else {
                        numLabel.top = centerFrame.origin.y + 6;
                    }
                    
                    // 水中关卡,第12关
                } else if (tag == 11) {
                    doneImage = cardModel.cardWaterDoneImage;
                    dontImage = cardModel.cardWaterDontImage;
                    ;
                    if ([card.shouldClick boolValue]) {
                        [card setImage:doneImage forState:UIControlStateNormal];
                        if ([ATUtil isiPad]) {
                            numLabel.top = centerFrame.origin.y + 15;
                        }
                    } else {
                        [card setImage:dontImage forState:UIControlStateNormal];
                        numLabel.top = card.bottom - ATFloat(10);
                    }
                    // 普通关卡
                } else {
                    if ([ATUtil isiPad]) {
                        if ([card.shouldClick boolValue]) {
                            numLabel.top = centerFrame.origin.y + 15;
                        }
                    }
                }
                // 第2个主题
            } else if (cardModel.cardStartIndex == 31) {
                doneImage = cardModel.cardSuperDoneImage;
                dontImage = cardModel.cardSuperDontImage;
                if ((tag == 33) || (tag == 49) || (tag == 59)) {
                    if ([ATUtil isiPad]) {
                        card.size = CGSizeMake(doneImage.size.width / 2, doneImage.size.height / 2);
                    }
                    if ([card.shouldClick boolValue]) {
                        card.center = centerFrame.origin;
                        [card setImage:doneImage forState:UIControlStateNormal];
                        if ([ATUtil isiPad]) {
                            numLabel.top = card.bottom - 25;
                        }
                    } else {
                        card.center = centerFrame.origin;
                        [card setImage:dontImage forState:UIControlStateNormal];
                        if ([ATUtil isiPad]) {
                            numLabel.top = card.bottom - 15;
                        } else {
                            numLabel.top = card.bottom - ATFloat(14);
                        }
                    }
                    // 普通关卡
                } else {
                    if ([card.shouldClick boolValue]) {
                        if ([ATUtil isiPad]) {
                            numLabel.top = centerFrame.origin.y + 15;
                        }
                    } else {
                        if ([ATUtil isiPad]) {
                            numLabel.top = centerFrame.origin.y + 30;
                        }
                    }
                }
                // 第3个主题
            } else if (cardModel.cardStartIndex == 62) {
                doneImage = cardModel.cardSuperDoneImage;
                dontImage = cardModel.cardSuperDontImage;
                if ((tag == 66) || (tag == 79) || (tag == 89)) {
                    if ([card.shouldClick boolValue]) {
                        if (scale == 1) {
                            card.size = CGSizeMake(doneImage.size.width / 2, doneImage.size.height / 2);
                        } else {
                            card.size = CGSizeMake(doneImage.size.width / scale, doneImage.size.height / scale);
                        }
                        
                        card.center = centerFrame.origin;
                        [card setImage:doneImage forState:UIControlStateNormal];
                        if ([ATUtil isiPad]) {
                            numLabel.top = card.centerY + 15;
                        } else {
                            numLabel.top = centerFrame.origin.y + 8;
                        }
                    } else {
                        if (scale == 1) {
                            card.size = CGSizeMake(dontImage.size.width / 2, dontImage.size.height / 2);
                        } else {
                            card.size = CGSizeMake(dontImage.size.width / scale, dontImage.size.height / scale);
                        }
                        
                        card.center = centerFrame.origin;
                        [card setImage:dontImage forState:UIControlStateNormal];
                        if ([ATUtil isiPad]) {
                            numLabel.top = card.centerY + 25;
                        } else {
                            if ([ATUtil isiPhoneSize_3_5] || [ATUtil isiPhoneSize_4_0]) {
                                numLabel.top = card.centerY + 10;
                            } else {
                                numLabel.top = card.centerY + 15;
                            }
                        }
                    }
                    // 普通关卡
                } else {
                    if ([ATUtil isiPad]) {
                        if ([card.shouldClick boolValue]) {
                            numLabel.top = centerFrame.origin.y + 15;
                        } else {
                            numLabel.top = centerFrame.origin.y + 22;
                        }
                    }
                }
                // 第4个主题
            } else if (cardModel.cardStartIndex == 93) {
                doneImage = cardModel.cardSuperDoneImage;
                dontImage = cardModel.cardSuperDontImage;
                
                if ((tag == 99) || (tag == 119)) {
                    if ([card.shouldClick boolValue]) {
                        if ([ATUtil isiPad]) {
                            card.size = CGSizeMake(doneImage.size.width / 2, doneImage.size.height / 2);
                        }
                        card.center = centerFrame.origin;
                        [card setImage:doneImage forState:UIControlStateNormal];
                    } else {
                        if ([ATUtil isiPad]) {
                            card.size = CGSizeMake(dontImage.size.width / 2, dontImage.size.height / 2);
                        }
                        card.center = centerFrame.origin;
                        [card setImage:dontImage forState:UIControlStateNormal];
                    }
                }
                if ([ATUtil isiPad]) {
                    if ([card.shouldClick boolValue]) {
                        numLabel.top = centerFrame.origin.y + 15;
                    } else {
                        numLabel.top = card.bottom - 20;
                    }
                }
                // 第5个主题
            } else if (cardModel.cardStartIndex == 124) {
                doneImage = cardModel.cardSuperDoneImage;
                dontImage = cardModel.cardSuperDontImage;
                
                if ((tag == 133) || (tag == 149)) {
                    
                    if ([ATUtil isiPad]) {
                        card.size = CGSizeMake(doneImage.size.width / 2, doneImage.size.height / 2);
                    }
                    if ([card.shouldClick boolValue]) {
                        card.center = centerFrame.origin;
                        [card setImage:doneImage forState:UIControlStateNormal];
                        if ([ATUtil isiPad]) {
                            numLabel.top = card.bottom - 25;
                        }
                    } else {
                        card.center = centerFrame.origin;
                        [card setImage:dontImage forState:UIControlStateNormal];
                        if ([ATUtil isiPad]) {
                            numLabel.top = card.bottom - 15;
                        } else {
                            numLabel.top = card.bottom - ATFloat(14);
                        }
                    }
                    // 普通关卡
                } else {
                    if ([card.shouldClick boolValue]) {
                        if ([ATUtil isiPad]) {
                            numLabel.top = centerFrame.origin.y + 15;
                        }
                    } else {
                        if ([ATUtil isiPad]) {
                            numLabel.top = centerFrame.origin.y + 30;
                        }
                    }
                }
                // 第6个主题
            } else if (cardModel.cardStartIndex == 155) {
                doneImage = cardModel.cardSuperDoneImage;
                dontImage = cardModel.cardSuperDontImage;
                if (tag == 159) {
                    if ([card.shouldClick boolValue]) {
                        if (scale == 1) {
                            card.size = CGSizeMake(doneImage.size.width / 2, doneImage.size.height / 2);
                        } else {
                            card.size = CGSizeMake(doneImage.size.width / scale, doneImage.size.height / scale);
                        }
                        
                        card.center = centerFrame.origin;
                        [card setImage:doneImage forState:UIControlStateNormal];
                        if ([ATUtil isiPad]) {
                            numLabel.top = card.centerY + 15;
                        } else {
                            numLabel.top = centerFrame.origin.y + 8;
                        }
                        
                    } else {
                        if (scale == 1) {
                            card.size = CGSizeMake(dontImage.size.width / 2, dontImage.size.height / 2);
                        } else {
                            card.size = CGSizeMake(dontImage.size.width / scale, dontImage.size.height / scale);
                        }
                        card.center = centerFrame.origin;
                        [card setImage:dontImage forState:UIControlStateNormal];
                        if ([ATUtil isiPad]) {
                            numLabel.top = card.centerY + 25;
                        } else {
                            if ([ATUtil isiPhoneSize_3_5] || [ATUtil isiPhoneSize_4_0]) {
                                numLabel.top = card.centerY + 10;
                            } else {
                                numLabel.top = card.centerY + 15;
                            }
                        }
                    }
                } else {
                    if ([ATUtil isiPad]) {
                        if ([card.shouldClick boolValue]) {
                            numLabel.top = centerFrame.origin.y + 15;
                        } else {
                            numLabel.top = centerFrame.origin.y + 22;
                        }
                    }
                }
            }
            
            tag += 1;
        }];
;
    }
    [self.contentView sendSubviewToBack:self.bgImageView];
}

- (void)onTapCardAction:(UIButton *)btn {
    NSLog(@"🐱🐱🐱:===tag:%ld=====frame:%@=====>单词关卡被点击了!!!", btn.tag, NSStringFromCGRect(btn.frame));
    if ([self.deleagte respondsToSelector:@selector(cardButtonClickedButton:withIndex:)]) {
        [self.deleagte cardButtonClickedButton:btn withIndex:btn.tag + 1];
    }
}

@end
